﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.File
{
    public partial class ucUsers : UserControl
    {
        #region UserControl

        public ucUsers()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            loadUsers();
            this.pList.BringToFront();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = Main.AeroColor;
        }

        #endregion

        #region Event

        #region Button

        private void btnUsers_Click(object sender, EventArgs e)
        {
            this.pList.BringToFront();
            this.btnAdd.Enabled = true;
            this.btnModify.Enabled = true;
            this.btnDelete.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.tbUserID.Text = "";
            this.tbPass.Text = "";
            this.tbPassConfirm.Text = "";
            this.cbEnable.Checked = true;
            this.tbUserName.Text = "";
            this.rbtnMale.Checked = true;
            this.tbComment.Text = "";
            this.pUser.Tag = "";

            this.pUser.BringToFront();
            this.btnModify.Enabled = false;
            this.btnDelete.Enabled = false;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的用户");
                return;
            }
            if (this.dgvUsers.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一个用户");
                return;
            }

            this.tbUserID.Text = this.dgvUsers.SelectedRows[0].Cells[0].Value.ToString();
            this.tbUserName.Text = this.dgvUsers.SelectedRows[0].Cells[1].Value.ToString();
            if (this.dgvUsers.SelectedRows[0].Cells[5].Value.ToString().ToLower() == "true") this.cbEnable.Checked = true;
            else this.cbEnable.Checked = false;
            if (this.dgvUsers.SelectedRows[0].Cells[4].Value.ToString() == "男") this.rbtnMale.Checked = true;
            else this.rbtnFemale.Checked = true;
            this.tbComment.Text = this.dgvUsers.SelectedRows[0].Cells[3].Value.ToString();
            this.pUser.Tag = this.dgvUsers.SelectedRows[0].Cells[6].Value.ToString();

            this.pUser.BringToFront();
            this.btnAdd.Enabled = false;
            this.btnDelete.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的用户");
                return;
            }

            List<string> guids = new List<string>();
            foreach (DataGridViewRow row in this.dgvUsers.SelectedRows) guids.Add(row.Cells[6].Value.ToString());

            int rval = MoneyClass.User.Delete(guids);

            if (rval < 1)
            {
                MessageBox.Show("操作失败");
                return;
            }

            loadUsers();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.tbUserID.Text.Length == 0)
            {
                MessageBox.Show("请输入账号");
                return;
            }
            if ((this.pUser.Tag == null || string.IsNullOrEmpty(this.pUser.Tag.ToString())) && this.tbPass.Text.Length == 0)
            {
                MessageBox.Show("请输入密码");
                return;
            }
            if (this.tbPass.Text != this.tbPassConfirm.Text)
            {
                MessageBox.Show("两次输入密码不一致");
                return;
            }

            int rval = 0;
            if (this.pUser.Tag == null || string.IsNullOrEmpty(this.pUser.Tag.ToString())) rval = MoneyClass.User.Add(this.tbUserID.Text, QT.Tools.MD5(this.tbPass.Text), this.tbUserName.Text, this.tbComment.Text, this.cbEnable.Checked, this.rbtnMale.Checked);
            else rval = MoneyClass.User.Update(this.pUser.Tag.ToString(), this.tbUserID.Text, string.IsNullOrEmpty(this.tbPass.Text) ? null : QT.Tools.MD5(this.tbPass.Text), this.tbUserName.Text, this.tbComment.Text, this.cbEnable.Checked, this.rbtnMale.Checked);

            switch (rval)
            {
                case -2:
                    MessageBox.Show("用户名已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("保存失败");
                    break;
                default:
                    MessageBox.Show("操作成功");
                    loadUsers();
                    btnUsers_Click(sender, e);
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnUsers_Click(sender, e);
        }

        #endregion

        #region DataGridView

        private void dgvUsers_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}个用户", this.dgvUsers.RowCount);
        }

        #endregion

        #endregion

        #region Method

        private void loadUsers()
        {
            DataTable dt = MoneyClass.User.getAll();
            this.dgvUsers.AutoGenerateColumns = false;
            this.dgvUsers.Columns[0].DataPropertyName = "userid";
            this.dgvUsers.Columns[1].DataPropertyName = "username";
            this.dgvUsers.Columns[2].DataPropertyName = "status";
            this.dgvUsers.Columns[3].DataPropertyName = "comment";
            this.dgvUsers.Columns[4].DataPropertyName = "xb";
            this.dgvUsers.Columns[5].DataPropertyName = "enable";
            this.dgvUsers.Columns[6].DataPropertyName = "guid";
            this.dgvUsers.DataSource = dt;
            this.dgvUsers.AutoResizeColumns();
        }

        #endregion
    }
}
